cc.Class({
    extends: cc.BaseClass,

    properties: {
        edibxAccount:       cc.EditBox,  
        edibxPassward:      cc.EditBox,
        togAgree:           cc.Toggle,
        prefabs:            [cc.Prefab],
        togLoginType:       [cc.Toggle],
    },

    onLoad () {

    },  

    start () {
        this.initData();
        this.registerEvent();
    },

    initData:function(){
        this.getCodeBtn = this.edibxPassward.node.parent.getChildByName("btnGetCode");
        this.getCodelab = this.edibxPassward.node.parent.getChildByName("labTime");
        this.getCodelab.active = false;
        this.edibxAccount.interactable = false;
        this.edibxPassward.interactable = true;
        this.phoneCode = 0;            //验证码 

        ClientNotify.init();                        //初始化本地通讯类
        var url = 'ws://'+ (G.isLocal ? G.ip : G.ip_line) +':'+ G.port;       //初始化网络
        G.netWork = new webSocket(url);
        this.sendHeart();  //测试
    },

    registerEvent:function(){
        ClientNotify.register(CLIENT_NOTIFY_TYPE.CONNECT_SUCCESS,this.onConnectSuccess,this);
        ClientNotify.register(CLIENT_NOTIFY_TYPE.LOGIN_RESPONSE,this.onLoaginResponse,this);
        ClientNotify.register(CLIENT_NOTIFY_TYPE.GOTOLOGIN,this.onRegisterSueccess,this);
        
    },

    onBtnClickCallBack:function(event,customData){
        var type = customData;
        if(type == "login"){
            this._onLogin();
        }else if(type == "register"){
            this._onRegister();
        }else if(type == "agree"){
            this._onAgree();
        }else if(type == "getCode"){
            this._onGetCode();
        }else if(type == "loginWX"){
            this._onLoginWX();
        }else if(type == "loginQQ"){
            this._onLoginQQ();
        }else if(type == "loginPhone"){
            this._onLoginPhone();
        }
    },

    onTogClickCallBack:function(event,customData){
        var type = customData;
        this._checkTogStatus();
        if(type == "togPwd"){
            this._onPwdLogin();
        }else if(type == "togCode"){
            this._onCodeLogin();
        }
    },

    _checkTogStatus:function(){
        for(let i = 0; i < this.togLoginType.length;i++){
            var item = this.togLoginType[i];
            item.interactable = !item.isChecked;
        }
    },

    _onLogin:function(code){
        var accout = this.edibxAccount.string;
        var password = this.edibxPassward.string;
        if(accout == ""){
            G.showTip(LNG.PLACELOGIN);
            return;  
        }
         if(password == ""){
            G.showTip(LNG.PLACEENTERPWD);
            return;  
        }
        if(this.togLoginType[1].isChecked){
            this.LoginPhone(accout,password, LOGINTYPE.PHONEPASSWORDREGISTER);
        }else{
            if(this.phoneCode != password){
                G.showTip(LNG.LOGINCODE_ERR);
                return; 
            }
            this.LoginPhone(accout,password, LOGINTYPE.PHONECODELOGINREDISTER);
        }
    },

    _onRegister:function(){
        this.createPre(1);
    },

    _onAgree:function(){
        this.createPre(0);
    },

    _onGetCode:function(){
        this.timer = GETCODETIME;
        var accout = this.edibxAccount.string;
        if(accout == "" || accout.length < 11){
            G.showTip(LNG.PLACELOGIN);
            return;  
        }

        this.getCodelab.active = true;
        this.getCodelab.getComponent(cc.Label).string = this.timer; 
        this.getCodeBtn.getComponent(cc.Button).interactable = false;

        this.schedule(this._setgetColdTimer,1);
        WebCenter.getPhoneCode(accout,function(data){
            var item = JSON.parse(data);
            if(item.Res == 1){
                this.phoneCode = item.RandNum;
            }
            G.showTip(item.Describe);  
        }.bind(this));
    },

    _setgetColdTimer :function(){
        this.timer--;
        if(this.timer < 0){
            this.unschedule(this._setgetColdTimer);
            this.getCodelab.active = false;
            this.getCodeBtn.getComponent(cc.Button).interactable = true;
        }
        this.getCodelab.getComponent(cc.Label).string = this.timer; 
    },

    _onLoginWX:function(){
        G.showTip(LNG.DEVELOP);

        // G.showTipPanel({
        //     data: LNG.PLACEAGREEPROTOCOL,
        //     callCancle: null,
        //     callConfirm:this.onConfirmAgree,
        //     target:this
        // }); 
    },

    _onLoginQQ:function(){
        G.showTip(LNG.DEVELOP);
    },

    _onLoginPhone:function(){
        this.createPre(1);
    },

    _onCodeLogin:function(){
        this.edibxPassward.maxLength = 4;
        this.getCodeBtn.active = true;
        this.edibxAccount.string = "";
        this.edibxPassward.string = "";
        this.edibxPassward.placeholder = LNG.LOGINHODER_CODE;
    },

    _onPwdLogin:function(){
        this.edibxPassward.maxLength = 18;
        this.getCodeBtn.active = false;
        this.edibxAccount.string = "";
        this.edibxPassward.string = "";
        this.edibxPassward.placeholder = LNG.LOGINHODER_PWD;
    },

    //手机号登录
    LoginPhone:function(accout,password,type){
        WebCenter.loginGameByPhone(accout,password,function(data){
            var item = JSON.parse(data);
            if(item.Res == 0){
                this.LoginAccount(item.Accounts,item.LogonPass,PLSTFROM_OTHER);
            }else{
                G.showTip(LNG.LOGINERROR);  
            }
        }.bind(this),type);
    },

    LoginAccount: function (Account, Password, Platform) {
        if (!this.CheckToggle()) return;
        LOGIN_PLSTFROM = Platform;
        // //进度条转圈
        // this.pBarSec.parent.active=true;
        // var repSec=cc.repeatForever(cc.rotateBy(1,360));
        // this.pBarSec.runAction(repSec);

        var LogonAccounts = new CMD_GP_LogonAccounts();
        LogonAccounts.szAccounts = Account;
        LogonAccounts.szPassword = Password;

        HallEvent.login(LogonAccounts);
        // var LoginMission = new CGPLoginMission(this, MDM_GP_LOGON, SUB_GP_LOGON_ACCOUNTS, LogonAccounts);
        // var pGlobalUserData=g_GlobalUserInfo.GetGlobalUserData();
        // pGlobalUserData.szPassword = LogonAccounts.szPassword;
        // cc.sys.localStorage.setItem(Key_LoginAcc, Account);
        // cc.sys.localStorage.setItem(Key_LoginPswT, Password);
        // cc.sys.localStorage.setItem(Key_LoginPlatfrom, Platform);
        // cc.sys.localStorage.setItem(Key_PhoneCode, null);
    },
    
    CheckToggle:function(){
        if(!this.togAgree.isChecked){
            G.showTipPanel({
                data: LNG.PLACEAGREEPROTOCOL,
                callCancle: null,
                callConfirm:this.onConfirmAgree,
                target:this
            }); 
            return false;
        }
        return true;
    },

    onConfirmAgree:function(target){
        console.log("=========onConfirm=========");
        target.togAgree.isChecked = true;
    },

    createPre:function(index){
        if(!this.prefabs[index]) return;
        var node = cc.instantiate(this.prefabs[index]);
        this.node.addChild(node);
    },

    onDestroy: function() {
        ClientNotify.removeByType(CLIENT_NOTIFY_TYPE.CONNECT_SUCCESS);
        ClientNotify.removeByType(CLIENT_NOTIFY_TYPE.LOGIN_RESPONSE);
        ClientNotify.removeByType(CLIENT_NOTIFY_TYPE.GOTOLOGIN);
        this.unschedule(this._setgetColdTimer);
    },

    //---------------------------服务器消息------------------------------
    onConnectSuccess:function(data,target){
        var self = target;
        console.log("=======onConnectSuccess=======");
        self.sendHeart();
    },
    
    onLoaginResponse:function(data,target){
        var self = target;
        console.log("=======onLoaginResponse======="+JSON.stringify(data));
        SwitchSceneManager.getInstance().loadSceneById(SCENE_ID.MAIN_MENU);
        this.phoneCode = 0;
    },

    //注册成功去登录
    onRegisterSueccess:function(data,target){
        var self = target;
        self.LoginAccount(data.Accounts,data.LogonPass,PLSTFROM_OTHER);
    }
});
